import Vue from "vue";
// 这里 import 进来的单文件组件只是一个单纯的配置对象
import MactionSheetCom from "./MactionSheet.vue";
// 必须以它的配置为基础，生成一个 Vue 子类
const MactionSheet = Vue.extend(MactionSheetCom);
export const ActionSheet = class A {
  constructor(options) {
    this.actionsheet = new MactionSheet({
      propsData: {
        ...options,
      },
    });
    this.actionsheet.$mount();
    this.node = this.actionsheet.$el;
    document.body.appendChild(this.node);
    this.actionsheet.$on("close", () => {
      if (options.onClose) {
        options.onClose();
      }
      this.close();
    });
    this.actionsheet.$on("cancel", () => {
      // console.warn(options);
      if (options.onCancel) {
        options.onCancel();
      }
    });
    this.actionsheet.$on("select", (item) => {
      // console.warn(options)
      if (!options.onSelect) {
        console.warn("没有设置选择函数");
      } else {
        options.onSelect(item);
      }
      this.close();
    });
  }
  show() {
    document.body.appendChild(this.node);
  }
  close() {
    this.node.remove();
    this.actionsheet.$destroy();
    this.node = null;
    // 完全销毁此实例，防止内存泄漏
    this.actionsheet.$destroy();
  }
};
export const ActionPlugin = {
  install(Vue) {
    Vue.$ActionSheet = Vue.prototype.$ActionSheet = ActionSheet;
  },
};
